/**
 * Copyright (c) 2012, David Varbel
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.epistrata.neutraltheory;

import static org.junit.Assert.*;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.epistrata.experiment.Run;

public class BooleanArrayCritterTest {
	Run run;
	NeutralTheoryModel model;
	
	@Before
	public void setUp() throws Exception {
		HashMap<String, String> factors = new HashMap<String, String>();
		factors.put("Dispersal Distance","1");
		factors.put("Dispersal Probability","0");
		factors.put("Early Death Probability","0");
		factors.put("Genetic Distance","2");
		factors.put("Minimum Possible Potential Mates","1");
		factors.put("Mutation Probability","0.5");
		factors.put("Spacial Distance","2");
		
		List<String> responseTypes = new ArrayList<String>();
		responseTypes.add("Recent Species Mean");
		responseTypes.add("Recent Species Variance");
		
		run = new Run(null, 1, 1, factors, responseTypes);
		model = new NeutralTheoryModel(run);

	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testMateWith() throws Exception {
		double xLoc = 100.0d;
		double yLoc = 100.0d;
		
		BooleanArrayCritter thisCritter = BooleanArrayCritter.newRandomInstance(model);
		Field thisGenomeField = thisCritter.getClass().getDeclaredField("genome");
		thisGenomeField.setAccessible(true);
		boolean[] thisGenome = {false, false, false, false, false, true, true, true, true, true};
		thisGenomeField.set(thisCritter, thisGenome);
		thisCritter.setLocation(xLoc, yLoc);

		BooleanArrayCritter otherCritter = BooleanArrayCritter.newRandomInstance(model);
		Field otherGenomeField = otherCritter.getClass().getDeclaredField("genome");
		otherGenomeField.setAccessible(true);
		boolean[] otherGenome = {true, true, true, true, true, false, false, false, false, false};
		otherGenomeField.set(otherCritter, otherGenome);
		
		model.getRandom().setSeed(1);
		
		BooleanArrayCritter offspring = (BooleanArrayCritter) thisCritter.mateWith(otherCritter);
		Field offspringGenomeField = offspring.getClass().getDeclaredField("genome");
		offspringGenomeField.setAccessible(true);
		boolean[] offspringGenome = (boolean[]) offspringGenomeField.get(offspring);
		
		boolean[] result = {true, true, true, false, false, true, true, false, false, true};
		for(int i = 0; i < offspringGenome.length; i++) {
			assertTrue("Mating did not return expected genome.", !(offspringGenome[i] ^ result[i]));
		}
		
		assertEquals("X location of offspring unexpected.", offspring.getLocation()[0], xLoc, 0.0);
		assertEquals("Y location of offspring unexpected.", offspring.getLocation()[1], yLoc, 0.0);
		
		//test dispersal
		thisCritter = BooleanArrayCritter.newRandomInstance(model);
		thisCritter.setLocation(xLoc, yLoc);
		Field dispersalField = model.getClass().getDeclaredField("dispersalProbability");
		dispersalField.setAccessible(true);
		dispersalField.set(model, 1.0d);
		offspring = (BooleanArrayCritter) thisCritter.mateWith(otherCritter);
		
		double distance =   Math.sqrt(((offspring.getLocation()[0] - xLoc) * (offspring.getLocation()[0] - xLoc) + 
							(offspring.getLocation()[1] - yLoc) * (offspring.getLocation()[1] - yLoc)));
		assertTrue("No dispersal of offspring when expected.", distance != 0.0d);
		assertTrue("Distance to dispersed offspring not less than max dispersal distance.", distance <= 1.0d);


	}

	@Test
	public void testMateOpportunistWith() throws Exception {
		double xLoc = 100.0d;
		double yLoc = 100.0d;

		BooleanArrayCritter thisCritter = BooleanArrayCritter.newRandomInstance(model);
		Field thisGenomeField = thisCritter.getClass().getDeclaredField("genome");
		thisGenomeField.setAccessible(true);
		boolean[] thisGenome = {false, false, false, false, false, true, true, true, true, true};
		thisGenomeField.set(thisCritter, thisGenome);
		thisCritter.setLocation(99.0d, 99.0d);

		BooleanArrayCritter otherCritter = BooleanArrayCritter.newRandomInstance(model);
		Field otherGenomeField = otherCritter.getClass().getDeclaredField("genome");
		otherGenomeField.setAccessible(true);
		boolean[] otherGenome = {true, true, true, true, true, false, false, false, false, false};
		otherGenomeField.set(otherCritter, otherGenome);
		
		BooleanArrayCritter earlyDeathCritter = BooleanArrayCritter.newRandomInstance(model);
		earlyDeathCritter.setLocation(xLoc, yLoc);
		
		model.getRandom().setSeed(1);
		
		BooleanArrayCritter offspring = (BooleanArrayCritter) thisCritter.mateOpportunistWith(otherCritter, earlyDeathCritter);
		Field offspringGenomeField = offspring.getClass().getDeclaredField("genome");
		offspringGenomeField.setAccessible(true);
		boolean[] offspringGenome = (boolean[]) offspringGenomeField.get(offspring);
		
		boolean[] result = {true, true, true, false, false, true, true, false, false, true};
		for(int i = 0; i < offspringGenome.length; i++) {
			assertTrue("Mating did not return expected genome.", !(offspringGenome[i] ^ result[i]));
		}
		
		assertEquals("X location of offspring unexpected.", offspring.getLocation()[0], xLoc, 0.0);
		assertEquals("Y location of offspring unexpected.", offspring.getLocation()[1], yLoc, 0.0);
	}

	@Test
	public void testIsSameSpecies() throws Exception {
		BooleanArrayCritter thisCritter = BooleanArrayCritter.newRandomInstance(model);
		Field thisGenomeField = thisCritter.getClass().getDeclaredField("genome");
		thisGenomeField.setAccessible(true);

		BooleanArrayCritter otherCritter = BooleanArrayCritter.newRandomInstance(model);
		Field otherGenomeField = otherCritter.getClass().getDeclaredField("genome");
		otherGenomeField.setAccessible(true);

		boolean[] thisGenome1 = {false, false, false, true, true, true};
		thisGenomeField.set(thisCritter, thisGenome1);
		boolean[] otherGenome1 = {false, false, false, false, true, true};
		otherGenomeField.set(otherCritter, otherGenome1);
		assertTrue("Critters are same species.", thisCritter.isSameSpecies(otherCritter, 1));
		
		boolean[] thisGenome2 = {false, false, false, true, true, true};
		thisGenomeField.set(thisCritter, thisGenome2);
		boolean[] otherGenome2 = {false, false, false, false, false, false};
		otherGenomeField.set(otherCritter, otherGenome2);
		assertTrue("Critters are same species.", thisCritter.isSameSpecies(otherCritter, 3));

		boolean[] thisGenome3 = {false, false, false, true, true, true};
		thisGenomeField.set(thisCritter, thisGenome3);
		boolean[] otherGenome3 = {false, true, false, false, false, false};
		otherGenomeField.set(otherCritter, otherGenome3);
		assertTrue("Critters are NOT same species.", !thisCritter.isSameSpecies(otherCritter, 3));
	}
	
	@Test
	public void testCrossover() throws Exception {
		
		BooleanArrayCritter thisCritter = BooleanArrayCritter.newRandomInstance(model);
		Field genome = thisCritter.getClass().getDeclaredField("genome");
		genome.setAccessible(true);
		boolean[] thisGenome = {false, false, false, true, true, true};
		genome.set(thisCritter, thisGenome);
		
		//override random generator
		model.getRandom().setSeed(1);
		
		Method crossover = thisCritter.getClass().getDeclaredMethod("crossover", boolean[].class);
		crossover.setAccessible(true);
		boolean[] mateGenome = {true, true, true, false, false, false};
		boolean[] offspringGenome = (boolean[]) crossover.invoke(thisCritter, mateGenome);
		
		boolean[] result = {false, false, false, false, false, false};
		for(int i = 0; i < offspringGenome.length; i++) {
			assertTrue("Crossover did not return expected genome.", !(offspringGenome[i] ^ result[i]));
		}
	}
	
	@Test
	public void testMutation() throws Exception {
		//override random generator
		model.getRandom().setSeed(2);
		
		BooleanArrayCritter thisCritter = BooleanArrayCritter.newRandomInstance(model);
		Method mutation = thisCritter.getClass().getDeclaredMethod("mutate", boolean[].class);
		mutation.setAccessible(true);
		boolean[] genome = {true, false};
		genome = (boolean[]) mutation.invoke(thisCritter, genome);
		boolean[] result = {false, false};
		for(int i = 0; i < genome.length; i++) {
			assertTrue("Mutation did not return expected genome.", !(genome[i] ^ result[i]));
		}

	}

}
